package service

import (
	v1 "helloworld/api/server/v1"
	"helloworld/internal/domain"

	"gitee.com/carlzyhuang/framework/rpc/websocket"
)

type WsGreeterService struct {
	v1.UnimplementedWsGreeterWsServer

	uc *domain.GreeterUsecase
}

func NewWsGreeterService(uc *domain.GreeterUsecase) *WsGreeterService {
	return &WsGreeterService{uc: uc}
}

// SayHello implements helloworld.GreeterWsServer.
func (s *WsGreeterService) SayHello(ctx websocket.SessionContext, in *v1.HelloRequest) (*v1.HelloReply, error) {
	g, err := s.uc.CreateGreeter(ctx.Context(), &domain.Greeter{Hello: in.Name})
	if err != nil {
		return nil, err
	}

	ctx.Send(v1.CMD_WsGreeter_SayHello, &v1.HelloReply{Message: "push message " + g.Hello, Code: 100})

	return &v1.HelloReply{Message: "response message " + g.Hello, Code: 200}, nil
}
